<template>
  <div class="day-panel">
    <div class="day-title">
      <div class="title-item" v-for="item in dayTitle" :key="item">
        <span>{{item}}</span>
      </div>
    </div>
    <div class="day-body">
      <div
        class="day-item"
        :class="getClassName(item.status)"
        v-for="(item, index) in dayList"
        :key="index"
      >
        <span>{{getDate(item.value)}}</span>
        <span class="lunar">{{getLunar(item.value)}}</span>
      </div>
    </div>
  </div>
</template>

<script>
import dayjs from 'dayjs'
import { inject, computed } from 'vue'
import LunarCalendar from 'lunar-calendar'
const DAY_COUNT = 42
const setStatus = (date, currentDate) => {
  if (date.isBefore(dayjs(currentDate).startOf('month'))) {
    return 'before'
  } else if (date.isAfter(dayjs(currentDate).endOf('month'))) {
    return 'after'
  } else if (date.isSame(dayjs(), 'date')) {
    return 'active'
  } else {
    return 'current'
  }
}
const getDayList = (currentDate) => {
  const subDay = dayjs(currentDate).startOf('month').day()
  const firstDay = dayjs(currentDate).startOf('month').subtract(subDay, 'day') // 第一个格子显示的日期
  let dayList = []
  for (let i = 0; i < DAY_COUNT; i++) {
    const value = firstDay.add(i, 'day')
    dayList.push({
      value,
      status: setStatus(value, currentDate)
    })
  }
  return dayList
}
export default {
  name: 'dayPanel',
  setup () {
    const dayTitle = ['日', '一', '二', '三', '四', '五', '六']
    const currentDate = inject('currentDate')
    const dayList = computed(() => getDayList(currentDate.value))
    const getDate = (date) => {
      return date.date()
    }
    const getClassName = (status) => {
      switch(status) {
        case 'before':
          return 'unactive'
        case 'after':
          return 'unactive'
        case 'active':
          return 'active'
      }  
    }
    const getLunar = (date) => {
      const year = date.year()
      const month = date.month()
      const day = date.date()
      const lunarObj = LunarCalendar.solarToLunar(year, month, day)
      return lunarObj.lunarFestival ?
                lunarObj.lunarFestival :
                // lunarObj.solarFestival ? lunarObj.solarFestival:
                lunarObj.term ? lunarObj.term:
                lunarObj.lunarDay === 1 ? lunarObj.lunarMonthName :
                lunarObj.lunarDayName
    }
    return {
      dayTitle,
      dayList,
      getDate,
      getClassName,
      getLunar
    }
  }
}
</script>

<style lang="less" scoped>
.day-panel {
  width: 100%;
  .day-title {
    height: 50px;
    display: flex;
    .title-item {
      width: 50px;
      height: 50px;
      text-align: center;
      line-height: 50px;
    }
  }
  .day-body {
    display: flex;
    flex-wrap: wrap;
    .day-item {
      width: 48px;
      height: 48px;
      text-align: center;
      display: flex;
      flex-direction: column;
      justify-content: center;
      font-weight: 500;
      margin: 1px;
      box-sizing: border-box;
      border: 2px solid #999;
      position: relative;
      &:hover {
        border: 2px solid rgba(255, 255, 255, 0.5);
        position: relative;
        z-index: 1;
      }
      .lunar {
        font-size: 10px;
        font-weight: normal;
        transform: scale(0.8);
        color: #ddd;
      }
    }
    .unactive {
      color: #999;
    }
    .active {
      border: 2px solid #007fff;
      background-color: #007fff;
      position: relative;
      z-index: 1;
    }
  }
  span {
    position: relative;
    z-index: 1;
  }
}
</style>